//
//  XHTabBar.m
//  BSNoAnswer
//
//  Created by Tiffany宝 on 16/5/13.
//  Copyright © 2016年 Tiffany. All rights reserved.
//

#import "XHTabBar.h"

@interface XHTabBar ()

@property (nonatomic,strong) UIButton *button;

@end

@implementation XHTabBar

-(instancetype)initWithFrame:(CGRect)frame{
    
    if (self == [super initWithFrame:frame]) {
        
        //创建撰写按钮
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        self.button = button;
        [button setBackgroundImage:[UIImage imageNamed:@"tabBar_publish_icon"] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageNamed:@"tabBar_publish_click_icon"] forState:UIControlStateHighlighted];
        //通过按钮当前图片的宽高来设置按钮的宽高
        CGFloat buttonW = button.currentBackgroundImage.size.width;
        CGFloat buttonH = button.currentBackgroundImage.size.height;
        button.frame = CGRectMake(0 ,0 ,buttonW ,buttonH );
        [self addSubview:button];
        
        //设置 tabbar 的背景图片 self = tabBar
        self.backgroundImage = [UIImage imageNamed:@"tabbar-light"];
        
    }
    return self;
}

-(void)layoutSubviews{
    
    [super layoutSubviews];
    
    [self setupUI];
    
}

-(void)setupUI{
    
    CGFloat itemW = XHScreenW / 5;
    CGFloat itemH = self.frame.size.height;
    CGFloat itemY = 0;
    CGFloat itemX = 0;
    NSInteger index = 0;
    for (UIView *item in self.subviews) {
        
        // 拿到 UITabBarButton
        if (![item isKindOfClass:NSClassFromString(@"UITabBarButton")]) continue;
        
        //            XHLog(@"%@",item);
        itemX = (index > 1) ? (index + 1) * itemW : index * itemW;
        item.frame = CGRectMake(itemX, itemY, itemW, itemH);
        index++;
        
    }
    self.button.center = CGPointMake(self.width * 0.5 , self.height * 0.5);
}

@end
